//
//  ProfessorViewController.m
//  beautifulHome
//
//  Created by lanou on 15/12/17.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "ProfessorViewController.h"
#import "LORequestManger.h"
#import "ProfessorModel.h"
#import "ProfessorTableViewCell.h"
#import "WorkTableViewCell.h"
#import "tableTiew1AndTableTiew2.h"
#import "BlurImageView.h"


#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

@interface ProfessorViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *array;

@property (nonatomic, strong) tableTiew1AndTableTiew2 *scrollView;

@property (nonatomic, strong) UISegmentedControl *segmentedControl;


@end

@implementation ProfessorViewController
// 懒加载
- (NSMutableArray *)array
{
    if (!_array) {
        _array = [[NSMutableArray alloc] init];
    }
    return _array;
}

- (void)jsonUrl {
    
    NSString *str = [NSString string];
    
    switch (_segmentedControl.selectedSegmentIndex) {
        case 0:
          str = @"http://api.meilijia.com/app/advanced_pro_list?page=2&sort=&zone=";
            break;
        case 1:
            str = @"http://api.meilijia.com/app/advanced_pro_gz_list?page=1&sort=&zone=";
            
        default:
            break;
    }
    
    [LORequestManger GET:str success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        
        NSDictionary *dataDict = dic[@"data"];
        NSDictionary *advancedDict = [NSDictionary dictionary];
        if (_segmentedControl.selectedSegmentIndex == 0) {
            advancedDict = dataDict[@"advanced_pro_list"];
        } else {
            advancedDict = dataDict[@"advanced_pro_gz_list"];

        }
          NSArray *listArray = advancedDict[@"list"];
        for (NSDictionary *dic in listArray) {
            ProfessorModel *model = [[ProfessorModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
            
        }
        
      [self.scrollView.tableView1 reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self jsonUrl];
    
     self.view.backgroundColor = [UIColor whiteColor];
    
    
//    BlurImageView *blurImageVC = [[BlurImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    
    // 创建分段控制器
    _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"设计师", @"工长"]];
    
    _segmentedControl.frame = CGRectMake(90, 25, 200, 30);
    _segmentedControl.tintColor = [UIColor orangeColor];
    _segmentedControl.selectedSegmentIndex = 0;
    
    [_segmentedControl addTarget:self action:@selector(changeControllAction:) forControlEvents:UIControlEventValueChanged];
    // 将分段控制器添加到导航控制器上
    [self.navigationController.view  addSubview:_segmentedControl];
    
    
    // 添加筛选按钮
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"筛选" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightAction)];
    // 改变筛选按钮颜色
    rightBarButton.tintColor = [UIColor orangeColor];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    
    // 添加城市按钮
    UIBarButtonItem *letfBarButton = [[UIBarButtonItem alloc] initWithTitle:@"城市 v" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction)];
    // 改变筛选按钮颜色
    letfBarButton.tintColor = [UIColor orangeColor];
    self.navigationItem.leftBarButtonItem = letfBarButton;
    
    
    
    self.scrollView = [[tableTiew1AndTableTiew2 alloc]initWithFrame:CGRectMake(0, 10, kWidth, kHeight)];
    
    [self.scrollView setTableViewTarget:self];
    [self.scrollView.tableView1 registerClass:[ProfessorTableViewCell class] forCellReuseIdentifier:@"professor"];
    
    [self.view addSubview:self.scrollView];
  
    // 隐藏单元格分割线
   self.scrollView.tableView1.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.scrollView.tableView2.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.scrollView.tableView1.backgroundColor = [UIColor yellowColor];
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)changeControllAction:(UISegmentedControl *)segmentedControl {
    
    // 必须先清除数组里的数据，否则会出现重复单元格，并且设计师和工长的单元格出现在同一个界面
    [self.array removeAllObjects];
    
    
    [self jsonUrl];
    
}


- (void)rightAction {
    
}

- (void)leftAction {
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 210;
}

// 加载单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
       ProfessorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"professor" forIndexPath:indexPath];
        
        ProfessorModel *model = _array[indexPath.row];
    
     cell.model = model;
    
    if (_segmentedControl.selectedSegmentIndex == 0) {
        // 需要先将工长界面的评价label的内容置空，否则会出现重复数据问题
        cell.reviewLabel.text = nil;
        cell.reviewStarLabel.text =  nil;
        
       cell.priceLabel.text = [NSString stringWithFormat:@"￥%@ - %@ /平米", model.pro_price_min, model.pro_price_max];
     //  cell.priceLabel.backgroundColor = [UIColor yellowColor];
       
        cell.priceLabel.textColor= [UIColor orangeColor];
        cell.verifiedLabel.text = [NSString stringWithFormat:@"%@ 套已验证作品", model.verified_work_count];
        
    } else {
        // 需要先将设计师界面的价格label的内容置空，否则会出现重复数据问题
        cell.priceLabel.text = nil;
      //  cell.priceLabel.backgroundColor = [UIColor whiteColor];
        
        
       cell.reviewLabel.text = [NSString stringWithFormat:@"%@条评论", model.review_count];
        cell.reviewStarLabel.text = [NSString stringWithFormat:@"评分: %@", model.pro_ratings];
        cell.reviewStarLabel.textColor = [UIColor orangeColor];
       cell.verifiedLabel.text = [NSString stringWithFormat:@"%@ 个已验证项目", model.verified_work_count];
        
    }
    
       return cell;
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
